package com.hty.PowerChat;

import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import pclientbackend.ChatClient;
import pclientbackend.Pclientbackend;

public class RegisterActivity extends AppCompatActivity {

    EditText editText_name, editText_password, editText_password_confirm, editText_desc;
    Button button_register_confirm, button_register_cancel;
    Spinner spinner_year;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("注册新用户");
        editText_name = findViewById(R.id.editText_register_name);
        editText_password = findViewById(R.id.editText_register_password);
        editText_password_confirm = findViewById(R.id.editText_password_confirm);
        editText_desc = findViewById(R.id.editText_register_desc);
        button_register_confirm = findViewById(R.id.button_register_confirm);
        button_register_cancel = findViewById(R.id.button_register_cancel);
        button_register_confirm.setOnClickListener(new ClickListener());
        button_register_cancel.setOnClickListener(new ClickListener());
        spinner_year = findViewById(R.id.spinner_register_birthyear);
        List<Long> list_year = new ArrayList<>();
        Calendar date = Calendar.getInstance();
        long year_now = date.get(Calendar.YEAR);
        for(long i = year_now-3; i>year_now-100; i--) {
            list_year.add(i);
        }
        ArrayAdapter<Long> adapter= new ArrayAdapter<>(this, R.layout.spinner_item, list_year);
        spinner_year.setAdapter(adapter);
        spinner_year.setSelection(20,true);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) { // ActionBar 返回键
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.button_register_confirm:
                    if(editText_password.getText().toString().equals(editText_password_confirm.getText().toString()) && !editText_password.getText().toString().equals("") && !editText_name.getText().toString().equals("")) {
                        long sex = 1;
                        if(spinner_year.getSelectedItem().toString().equals("女")){
                            sex = 2;
                        }
                        String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "PowerChat";
                        File dir = new File(path);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                        String json = "{\"Host\":\"148.70.58.15:7889\"}";
                        ChatClient chatClient = Pclientbackend.getChatClient(path, json);
                        if(chatClient.newUser(editText_name.getText().toString(), editText_password.getText().toString(), sex, (Long)spinner_year.getSelectedItem(), editText_desc.getText().toString())){
                            new AlertDialog.Builder(RegisterActivity.this).setIcon(R.mipmap.ic_launcher).setTitle("提示").setMessage("注册成功!").setPositiveButton("确定", null).show();
                        }else{
                            new AlertDialog.Builder(RegisterActivity.this).setIcon(android.R.drawable.stat_notify_error).setTitle("错误").setMessage("账号已经被注册！").setPositiveButton("确定", null).show();
                        }
                    }else{
                        new AlertDialog.Builder(RegisterActivity.this).setIcon(android.R.drawable.stat_notify_error).setTitle("错误").setMessage("用户名为空，密码为空，或者两次输入的密码不一致！").setPositiveButton("确定", null).show();
                    }
                    break;
                case R.id.button_register_cancel:
                    finish();
                    break;
            }
        }
    }

}